package android.database;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;
	 
    private static final String DATABASE_NAME = "PasswordData";
 
    private static final String TABLE = "Data";
    
    private static final String ID = "Id";
    
    private static final String basename = "Base_name";
    
    private static final String password = "Password";
    
    private static final String enc_data = "Encrypted_data";
    
	public Database(Context con)
	{
		super(con, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + basename + " TEXT, " + password + " TEXT, " + enc_data + " TEXT " + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE);
        onCreate(db);
    }
    
    public boolean checkPassword(String base, String pass)
    {
    	try 
    	{
    	SQLiteDatabase db = this.getReadableDatabase();
    	Cursor c = db.query(TABLE, new String[] { password }, basename + " = ?", new String[] { base }, null, null, null);
    	String db_pass = "";
    	while (c.moveToNext()) db_pass = c.getString(0);
    	db.close();
    	MessageDigest m = MessageDigest.getInstance("SHA-512");
		m.update(pass.getBytes());
		byte[] temp = m.digest();
		String hash_pass = new String(temp);
		if (hash_pass.equals(db_pass)) return true;
		else return false;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
    }
    
	public void changePassword(int index, String pass)
	{
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(password, pass);
		db.update(TABLE, values, ID + " = ?", new String[] {String.valueOf(index)});
		db.close();
	}
	
	public boolean addBase(String base, String pass)
	{
		try {
			List<String> bases = getBasesNames();
			for (int i = 0; i < bases.size(); i++) if (base.equals(bases.get(i))) return false;
			MessageDigest m = MessageDigest.getInstance("SHA-512");
			m.update(pass.getBytes());
			byte[] temp = m.digest();
			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put(basename, base);
			values.put(password, new String(temp));
			values.put(enc_data, "");
			db.insert(TABLE, null, values);
			db.close();
			return true;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public List<String> getBasesNames() 
	{
		List<String> bases = new ArrayList<String>();
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.query(TABLE, new String[] { basename }, null, null, null, null, null);
		while (c.moveToNext()) bases.add(c.getString(0));
		db.close();
		return bases;
	}
	
	public void updateData(int index, String data)
	{
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(enc_data, data);
		db.update(TABLE, values, ID + " = ?", new String[] {String.valueOf(index)});
		db.close();
	}
	
	public int getIndex(String base)
	{
		int index = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.query(TABLE, new String[] { ID }, basename + " = ?", new String[] { base }, null, null, null);
		while (c.moveToNext()) index = c.getInt(0); 
		db.close();
		return index;
	}
	
	public String getBaseName(int index)
	{
		String b = "";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.query(TABLE, new String[] { basename }, ID + " = ?", new String[] { String.valueOf(index) }, null, null, null);
		while (c.moveToNext()) b = c.getString(0);
		db.close();
		return b;
	}
	
	public String getPassword(int index)
	{
		String p = "";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.query(TABLE, new String[] { password }, ID + " = ?", new String[] { String.valueOf(index) }, null, null, null);
		while (c.moveToNext()) p = c.getString(0);
		db.close();
		return p;
	}
	
	public String getEncryptedData(int index)
	{
		String p = "";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.query(TABLE, new String[] { enc_data }, ID + " = ?", new String[] { String.valueOf(index) }, null, null, null);
		while (c.moveToNext()) p = c.getString(0);
		db.close();
		return p;
	}
}
